package b4.view
{
	import b4.ApplicationFacade;
	import b4.model.ResourceLoader;
	import b4.model.SmartBirdExtension;
	import b4.view.component.ChatDialog;
	import b4.view.component.admin.PaintDialog;
	import b4.view.component.main.MainFuncBar;
	import b4.view.component.town.RoleSprite;
	import b4.view.component.system.SystemFuncBar;
	import b4.view.component.task.TaskDialog;
	import b4.view.component.town.TownScene;
	import b4.view.component.user.UserPane;
	
	import flash.display.Sprite;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class ApplicationMediator extends Mediator implements IMediator
	{
		public static const NAME:String = 'ApplicationMediator';
		public static const ADD_CHILD:String = NAME + 'AddChild';
		public static const ADD_ROLE_SPRITE:String = NAME + 'AddRoleSprite';
		public static const SHOW_MOUSE_OVER_TIP:String = NAME + 'ShowMouseOverTip';
		public static const HIDE_MOUSE_OVER_TIP:String = NAME + 'HideMouseOverTip';
		
		public function ApplicationMediator(viewComponent:Object=null)
		{
			super( NAME, viewComponent );
		}
		
		override public function onRegister():void
		{
			facade.registerMediator(new TownSceneMediator(new TownScene()));
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				ApplicationMediator.ADD_CHILD,
//				ApplicationFacade.MAINROLE_LOADED,
				ApplicationFacade.RESOURCE_LOADED,
				ApplicationMediator.SHOW_MOUSE_OVER_TIP,
				ApplicationMediator.HIDE_MOUSE_OVER_TIP
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var body:Object = notification.getBody();
			var type:String = notification.getType();
			switch ( name )
			{
				case ApplicationMediator.ADD_CHILD:
					viewComponent.addChild(body);
				break;
				case ApplicationMediator.ADD_ROLE_SPRITE:
					var userId:int = body as int;
					if (!facade.hasMediator(RoleSpriteMediator.NAME + userId))
					{
						facade.registerMediator(new RoleSpriteMediator(userId));
					}
				break;
				case ApplicationFacade.RESOURCE_LOADED:
					if (type == ResourceLoader.TYPE_MAIN)
					{
						smartBirdExtension.connect();
						facade.registerMediator(new ChatDialogMediator(new ChatDialog()));
						facade.registerMediator(new MainFuncBarMediator(new MainFuncBar()));
						facade.registerMediator(new SystemFuncBarMediator(new SystemFuncBar()));
						facade.registerMediator(new UserPaneMediator(new UserPane()));
						facade.registerMediator(new PaintDialogMediator(new PaintDialog()));
						facade.registerMediator(new TaskDialogMediator(new TaskDialog()));
					}
				break;
				case ApplicationMediator.SHOW_MOUSE_OVER_TIP:
					mainStage.showMouseTip(body as Sprite);
				break;
				case ApplicationMediator.HIDE_MOUSE_OVER_TIP:
					mainStage.hideMouseTip();
				break;
			}
		}
		
		private function get smartBirdExtension():SmartBirdExtension
		{
			return facade.retrieveProxy( SmartBirdExtension.NAME ) as SmartBirdExtension;
		}

		public function get mainStage():B4Pro1
		{
			return viewComponent as B4Pro1;
		}
	}
}